package org.aeon.client.view.components.listColumn;

import lombok.Getter;
import org.aeon.client.context.GlobalContext;
import org.aeon.client.view.ApplicationUI;
import org.aeon.client.view.components.button.CheckButton;
import org.aeon.client.view.components.button.OperationButton;
import org.aeon.client.view.components.textField.SearchField;
import org.aeon.entity.User;
import org.aeon.entity.dto.UserDto;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author: MissingAeon
 * @date: 2024/2/29 8:49
 * @description:
 */
public class GroupCreationDialog extends JPanel {
    private ColumnScrollPanel rightScroll;
    private OperationButton cancelBut;
    private OperationButton submitBut;
    private ColumnScrollPanel leftScroll;
    @Getter
    private List<CheckButton> buttons;
    private JLabel label;
    private int count = 0;

    public GroupCreationDialog() {
        this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
        final ApplicationUI mainUI = GlobalContext.getContext().getMainUI();
        this.setBounds((mainUI.getWidth() - 600) / 2, (mainUI.getHeight() - 600) / 2, 600, 600);
        this.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.GRAY));

        this.add(left());

        // 创建垂直分隔线
        JSeparator separator = new JSeparator(SwingConstants.VERTICAL);
        // 设置分隔线的最大尺寸
        separator.setMaximumSize(new Dimension(1, Integer.MAX_VALUE));
        separator.setForeground(Color.lightGray);
        this.add(separator);

        this.add(right());
    }

    private Box right() {
        final Box box = Box.createVerticalBox();
        box.add(Box.createVerticalStrut(15));

        final Box titleBox = Box.createHorizontalBox();
        label = new JLabel("创建群聊");
        label.setFont(new Font("黑体", Font.PLAIN, 14));

        titleBox.add(Box.createHorizontalStrut(20));
        titleBox.add(label);
        titleBox.add(Box.createHorizontalGlue());
        box.add(titleBox);

        box.add(Box.createVerticalStrut(10));

        rightScroll = new ColumnScrollPanel(new Dimension(300, 500));
        rightScroll.setBorder(null);
        box.add(rightScroll);

        box.add(Box.createVerticalStrut(20));

        final Box butBox = Box.createHorizontalBox();

        butBox.add(Box.createHorizontalGlue());
        submitBut = new OperationButton("确认", new Color(0x10ABDA));
        butBox.add(submitBut);
        submitBut.setEnabled(false);
        butBox.add(Box.createHorizontalStrut(10));
        cancelBut = new OperationButton("取消", Color.gray);
        butBox.add(cancelBut);
        butBox.add(Box.createHorizontalStrut(25));


        box.add(butBox);
        box.add(Box.createVerticalStrut(20));
        return box;
    }

    private Box left() {
        final Box box = Box.createVerticalBox();
        box.add(Box.createVerticalStrut(10));

        final SearchField searchField = new SearchField(new Dimension(280, 30));
        box.add(searchField);

        box.add(Box.createVerticalStrut(10));

        leftScroll = new ColumnScrollPanel(new Dimension(300, 500));
        box.add(leftScroll);

        return box;
    }

    /**
     * 添加提交处理
     */
    public void addSubmitHandle(Runnable program) {
        submitBut.addActionListener(e -> program.run());
    }


    /**
     * 添加取消处理
     */
    public void addCancelHandle(Runnable program) {
        cancelBut.addActionListener(e -> program.run());
    }

    /**
     * 添加数据
     *
     * @param users
     * @param title
     */
    public void dataFilling(List<UserDto> users, String title) {
        label.setText("创建群聊：" + title);
        buttons = new ArrayList<>();
        final User currentUser = GlobalContext.getContext().getCurrentUser();
        users.forEach(user -> {
            final CheckButton checkBox = CheckButton.createCheckBox(
                    new Dimension(250, 50), user, !Objects.equals(currentUser.getId(), user.getId()));
            buttons.add(checkBox);
            leftScroll.addItem(checkBox);
            checkBox.addActionListener(e -> {
                if (!checkBox.isCheck()) {
                    if (++count > 0) submitBut.setEnabled(true);
                    if (checkBox.getAnother() != null) {
                        // 添加
                        rightScroll.addItem(checkBox.getAnother());
                        return;
                    }
                    final CheckButton removeBox = CheckButton.createRemoveBox(new Dimension(250, 50),
                            ((CheckButton) e.getSource()).getContent());
                    removeBox.addActionListener(ex -> checkBox.doClick());
                    // 绑定
                    checkBox.setAnother(removeBox);
                    removeBox.setAnother(checkBox);
                    // 添加
                    rightScroll.addItem(removeBox);
                } else if (checkBox.getAnother() != null) {
                    if (--count < 1) submitBut.setEnabled(false);
                    rightScroll.removeItem(checkBox.getAnother());
                }
            });
        });
    }
}
